<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('页面按钮')" :prop="form.event=='actEnd'?'':'buttonCode'">
            <el-select v-model="form.buttonCode">
              <el-option v-for="item in buttonOption" :key="item.value" :label="item.title" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label="$t('事件状态')" prop="event">
            <el-radio-group v-model="form.event" @input="changeEvent">
              <el-radio label="start">开始</el-radio>
              <el-radio label="end">结束</el-radio>
              <el-radio label="actEnd">审批结束</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('类型')" prop="event">

            <el-radio-group v-model="form.javaType">
              <el-radio label="spring">spring</el-radio>
              <el-radio label="class">class</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('内容')" prop="javaValue">
            <el-input v-model="form.javaValue" :placeholder="$t('...')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('是否有效')" prop="isActive">
            <el-radio-group v-model="form.isActive">
              <el-radio label="Y">有效</el-radio>
              <el-radio label="N">无效</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:java" />
    </div>
  </div>
</template>

<script>
import { saveOnlCgformEnhanceJava, getOnlCgformEnhanceJava } from '@/api/system/desform/enhance/onlCgformEnhanceJava'
import { mapState } from 'vuex'

const buttonOption = [
  { title: '新增', value: 'add' },
  { title: '修改', value: 'edit' },
  { title: '删除', value: 'del' }
  // { title: '流程', value: 'act' }
]
export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        buttonCode: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      buttonOption: buttonOption,
      headId: ''
    }
  },
  computed: {
    ...mapState('dbAbout', ['head'])
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getOnlCgformEnhanceJava(data.id).then(response => {
          this.form = response.data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    initHeadId(id) {
      this.headId = id
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        buttonCode: undefined,
        javaType: 'spring',
        javaValue: undefined,
        event: 'end',
        formHeadId: undefined,
        isActive: 'Y',
        remark: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.form.formHeadId = this.head.id
          saveOnlCgformEnhanceJava(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      }).catch(() => {
        this.loading = false
      })
    },
    changeEvent(val) {
       if (val === 'actEnd') {
          this.form.buttonCode = undefined
       }
    }
  }
}
</script>
